import network from "../../utils/network";
import util from "../../utils/util";

const app = getApp()
Page({
    data: {
        user_id: wx.getStorageSync('user_id'),
        yearData: {},
        active: 0,
        year: null,
        type: null,
        year_arr: [],
        saleSelectShow: false,
        selectUserLoading: true,
        userName: ''
    },
    onLoad: function (options) {
        const now = new Date()
        var year = now.getFullYear();
        var type = now.getMonth() > 5 ? 2 : 1
        let arr = []
        for (let i = 1980; i <= year; i++) {
            arr.push({
                name: i
            })
        }
        this.setData({
            year_arr: arr
        })
        if (options.type == 1) {
            this.setData({
                year: year,
                type: type
            })
            this.getData()
        } else if (options.type == 2) {
            this.setData({
                year: year,
                type: type,
                saleSelectShow: true,
                user_id: null
            })
            this.initSaleUserArr()
        }
    },
    initSaleUserArr() {
        network.reqPromise('GET', {
            uri: app.api.userListByCode,
            params: {code: 'Salesperson'}
        }).then(res => {
            this.setData({
                userList: res.data,
                selectUserLoading: false
            })
        }).catch(() => {
            util.showToast('初始化销售员数据失败')
        })
    },
    onChange(e) {
        const index = e.detail.index;
        const month = this.data.yearData.month_target_list[index].month;
        for (let i = 0; i < 4; i++) {
            this.selectComponent('#tabs' + month).resize();
        }
    },
    search() {
        if (!this.data.year) {
            util.showToast('请选择年份')
            return
        }
        if (!this.data.type) {
            util.showToast('请选择年度')
            return
        }
        if (!this.data.user_id) {
            util.showToast('请选择销售员')
            return
        }
        this.getData()
    },
    getData() {
        const now = new Date();
        util.showLoading('加载中')
        network.reqPromise('GET', {
            uri: app.api.querySaleYearTargetSummary,
            params: {
                year: this.data.year,
                type: this.data.type,
                user_id: this.data.user_id
            }
        }).then(res => {
            if (res.data.month_target_list) {
                res.data.month_target_list.forEach(item => {
                    if (item.week_completion_detail_list) {
                        item.week_completion_detail_list.forEach(tmp => {
                            tmp.myData = tmp.weekCompletionTypeVOList.find(temp => temp.type === 2);
                            tmp.companyData = tmp.weekCompletionTypeVOList.find(temp => temp.type === 1);
                        })
                    }
                })
            }
            this.setData({
                yearData: res.data,
                active: (now.getMonth() + 1) + ''
            })
            util.hideLoading()
        }).catch(err => {
            util.showToast(err.message ? err.message : '加载失败')
            util.hideLoading()
        })
    },
    onRadioChange(e) {
        this.setData({
            type: e.detail
        })
    },
    selectYearConfirm(e) {
        this.setData({
            year: e.detail.value.name
        })
    },
    selectUserConfirm(e) {
        this.setData({
            user_id: e.detail.value.id,
            userName: e.detail.value.name
        })
    }
});
